package com.hdkj.serviceai.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdkj.serviceai.conutils.ConUtil ;
import com.hdkj.serviceai.service.KuaidiService;
import com.hdkj.serviceai.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class KuaidiServiceImpl implements KuaidiService {
    @Override
    public Object getkuaidiinfo(Map<String, Object> map) {
        String host = "https://jisukdcx.market.alicloudapi.com";
        String path = "/express/query";
        String method = "get";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConUtil.getkey());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, Object> querys = new HashMap<String, Object>();
        querys.put("mobile", map.get("mobile"));
        querys.put("number", map.get("number"));
        querys.put("type", "auto");

        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            // System.out.println(EntityUtils.toString(response.getEntity(),"utf-8"));
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
            return jsonObject.get("result");
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }
}
